#!/bin/bash

## Adjust JAVA_HOME if necessary
#JAVA_HOME=/opt/jdk1.8.0_181

SERVICE_NAME=springboot_es
PIDFILE=$SERVICE_NAME.pid

PATH_TO_JAR=$SERVICE_NAME".jar"

## Adjust memory settings if necessary
#export JAVA_OPTS="-Xms6144m -Xmx6144m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=384m -XX:NewSize=4096m -XX:MaxNewSize=4096m -XX:SurvivorRatio=8"

## Only uncomment the following when you are using server jvm
#export JAVA_OPTS="$JAVA_OPTS -server -XX:-ReduceInitialCardMarks"

########### The following is the same for services ###########
#export JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=9 -XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -XX:+UseCMSInitiatingOccupancyOnly -XX:+ScavengeBeforeFullGC -XX:+UseCMSCompactAtFullCollection -XX:+CMSParallelRemarkEnabled -XX:CMSFullGCsBeforeCompaction=9 -XX:CMSInitiatingOccupancyFraction=60 -XX:+CMSClassUnloadingEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSPermGenSweepingEnabled -XX:CMSInitiatingPermOccupancyFraction=70 -XX:+ExplicitGCInvokesConcurrent -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationConcurrentTime -XX:+PrintHeapAtGC -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Duser.timezone=Asia/Shanghai -Dclient.encoding.override=UTF-8 -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom"

status() {
	if [ -f $PIDFILE ]; then
        PID=$(cat $PIDFILE)
        if [ ! -x /proc/${PID} ]; then
            return 1
        else
            return 0
        fi
    else
        return 1
    fi
}


cd `dirname $0`/..

for i in `ls $SERVICE_NAME-*.jar 2>/dev/null`
do
    if [[ ! $i == *"-sources.jar" ]]
    then
        PATH_TO_JAR=$i
        break
    fi
done

if [[ ! -f PATH_TO_JAR && -d current ]]; then
    cd current
    for i in `ls $SERVICE_NAME-*.jar 2>/dev/null`
    do
        if [[ ! $i == *"-sources.jar" ]]
        then
            PATH_TO_JAR=$i
            break
        fi
    done
fi

if [[ -f $SERVICE_NAME".jar" ]]; then
  rm -rf $SERVICE_NAME".jar"
fi

ln $PATH_TO_JAR $SERVICE_NAME".jar"
chmod a+x $SERVICE_NAME".jar"

case "$1" in
    start)
    	status
    	RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			echo "$PIDFILE exists, process is already running or crashed"
			exit 1
		fi
		
        echo "$(date) starting $SERVICE_NAME ..."
        ## with coverage open ##
        java -server -jar $SERVICE_NAME".jar" &
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			echo "$SERVICE_NAME is started"
			echo $! > $PIDFILE
			exit 0
		else
			echo "Stopping $SERVICE_NAME"
			rm -f $PIDFILE
			exit 1
		fi
        ;;
    stop)
    	status
    	RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			echo "Shutting down $SERVICE_NAME"
			kill `cat $PIDFILE`
			RETVAL=$?
			if [ $RETVAL -eq 0 ]; then
				rm -f $PIDFILE
			else
				echo "Failed to stopping $SERVICE_NAME"
			fi
		fi
        ;;
    status)
    	status
    	RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
	    	PID=$(cat $PIDFILE)
			echo "$SERVICE_NAME is running ($PID)"
		else
			echo "$SERVICE_NAME is not running"
		fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
		echo "Usage: $0 {start|stop|restart|status}"
		;;
esac
